package com.coder4j.auth.mapper;

import com.coder4j.auth.vo.MenuVO;
import com.coder4j.starter.mybatis.BaseMapper;
import com.coder4j.auth.entity.AuthResourceEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.List;

/**
 * 资源
 *
 * @author keller
 * @date 2024-05-07 18:10:11
 */
@Mapper
public interface AuthResourceMapper extends BaseMapper<AuthResourceEntity> {
    /**
     * 默认查询条件
     *
     * @return
     */
    @Override
    default List<String> defaultCondition() {
        return Arrays.asList(
                " <if test = 'createId!=null'> AND create_id=#{createId} </if>",
                " <if test = 'updateId!=null'> AND update_id=#{updateId} </if>",
                " <if test = 'description!=null'> AND description=#{description} </if>",
                " <if test = 'name!=null'> AND name=#{name} </if>",
                " <if test = 'type!=null'> AND type=#{type} </if>");
    }

    @Override
    default String defaultOrder() {
        return "group_id,sort";
    }

    /**
     * 删除所有资源
     */
    @Delete({"DELETE FROM auth_resource"})
    void deleteAll();

    /**
     * 查询菜单列表
     *
     * @return
     */
    @Select({
            "SELECT r.id ,r.name ,r.icon_name,r.web_path,g.id groupId,g.parent_id groupParentId,g.name groupName ,g.icon_name groupIcon",
            "FROM auth_resource r LEFT JOIN sys_group g  ON r.group_id = g.id ",
            "WHERE r.type = 'Menu' ORDER BY g.sort,r.sort"
    })
    List<MenuVO> queryMenuList();
}
